﻿using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkitApp.Services;
using CommunityToolkitApp.ViewModels;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Windows;

namespace CommunityToolkitApp
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// 静态全局的当前实例
        /// </summary>
        public new static App Current = (App)Application.Current;

        /// <summary>
        /// 容器
        /// </summary>
        public IServiceProvider ServiceProvider { get; private set; }

        /// <summary>
        /// 重写启动方法
        /// </summary>
        /// <param name="e"></param>
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            ConfigureService();
        }

        /// <summary>
        /// 配置服务
        /// </summary>
        private void ConfigureService()
        {
            // 创建一个 ServiceCollection 实例
            var services = new ServiceCollection();

            // 注册服务
            services.AddTransient<MainWindowViewModel>();
            services.AddTransient<ITestService, TestService>();

            // 构建服务提供者
            ServiceProvider = services.BuildServiceProvider();

            // 设置 IocProvider
            Ioc.Default.ConfigureServices(ServiceProvider);
        }
    }
}
